package com.webserver.test;

import java.io.*;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * java.io.ByteArrayOutputStream 和 ByteArrayInputStream.
 * 字节数组输出与输入流
 * 他们是一对低级流，内部维护一个字节数组
 *
 * ByteArrayOutputStream通过该流写出的数据都会保存在内部维护的字节数组中。
 */
public class BAOSDemo {
    public static void main(String[] args) throws FileNotFoundException {
//        FileOutputStream fos = new FileOutputStream("pw.txt");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(baos, StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);


        pw.println("helloWorld");
        byte[] data = baos.toByteArray();
        System.out.println("长度为: "+data.length);
        System.out.println("内容为: "+ Arrays.toString(data));

        pw.println("think in java");
        data = baos.toByteArray();
        System.out.println("长度为: "+data.length);
        System.out.println("内容为: "+ Arrays.toString(data));
    }
}
